// pages/project_score_add/project_score_add.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    projectId: null,
    score: 0,
    description: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.userId,
      projectId: options.projectId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '添加积分'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '添加积分'
    }
  },

  /**
   * 输入值改变
   */
  bindInputChange(e) {
    const key = e.currentTarget.dataset.name;
    this.setData({
      [key]: e.detail.value
    })
  },

  /**
   * 点击添加积分
   */
  clickSaveAdd(e) {
    const handle = e.currentTarget.dataset.handle;
    if (handle==0) {
      wx.navigateBack({
        delta: 1,
      })
    } else {
      if (this.data.score<=0) {
        wx.showToast({
          title: '积分不能为空或小于 0',
          icon: 'none',
          duration: 1500
        })
        return;
      }

      wx.showToast({
        title: '正在添加...',
        icon: 'loading',
        duration: 30000,
        mask: true
      })

      // 提交积分
      const param = {
        projectId: this.data.projectId,
        userId: this.data.userId,
        addUserId: wx.getStorageSync('userId'), 
        score: this.data.score,
        description: this.data.description.trim()
      };
      request.GET('/project/saveProjectScore', param).then(res => {
        // console.log(res);
        if (res.data.resCode==200) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 1000
          })

          setTimeout(function() {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        } else {
          wx.showToast({
            title: '添加异常',
            icon: 'error',
            duration: 1500,
            mask: true
          })
        }
      })
    }
  },
  
})